package dsk.demo.product.controller.automarket;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dsk.demo.common.utils.R;
import dsk.demo.product.entity.CustomerLinkManEntity;
import dsk.demo.product.service.CustomerLinkManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("customerLinkMan")
public class CustomerLinkManController {

    @Autowired
    private CustomerLinkManService customerLinkManService;


    @PostMapping("/saveOrUpdate")
    public R save(@RequestBody CustomerLinkManEntity entity) {
        customerLinkManService.saveOrUpdate(entity);
        return R.ok();
    }

    @GetMapping("/downloadTemp")
    public void downloadTemp(HttpServletResponse response) throws IOException {
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
        //配置别名
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("area", "国家");
        map.put("name", "姓名");
        map.put("email", "邮箱");
        map.put("phone", "电话");
        map.put("companyId", "公司ID");
        writer.setHeaderAlias(map);
        //只写入配置了别名的属性
        writer.setOnlyAlias(true);

        CustomerLinkManEntity customerEntity = new CustomerLinkManEntity();
        List<CustomerLinkManEntity> rows = new ArrayList<>();
        rows.add(customerEntity);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
        //out为OutputStream，需要写出到的目标流

        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        response.setHeader("Content-Disposition", "attachment;filename=customerLinkMan_temp.xls");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    /**
     * excel批量上传
     */
    @RequestMapping("/excelUpload")
    public R saveList(MultipartFile file) {
        try {
            List<CustomerLinkManEntity> list = customerLinkManService.excel2CustomerLinkManEntityList(file);
            for (CustomerLinkManEntity customerLinkManEntity : list) {
                customerLinkManEntity.setEmail(customerLinkManEntity.getEmail().trim());
                customerLinkManEntity.setCompanyId(customerLinkManEntity.getCompanyId().trim());
            }
            customerLinkManService.insertBatch(list);
            return R.ok("上传成功");
        } catch (Exception e) {
            return R.error(501, "上传失败");
        }
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        customerLinkManService.removeById(id);
        return R.ok();
    }

    @GetMapping("/info/{id}")
    public R list(@PathVariable("id") String id) {
        QueryWrapper<CustomerLinkManEntity> query = new QueryWrapper<>();
        query.eq("company_id", id);
        List<CustomerLinkManEntity> list = customerLinkManService.list(query);
        return R.ok().put("data", list);
    }

}
